package com.example.lottery_system.service.activitystatus.operator;

import cn.hutool.core.collection.CollectionUtil;
import com.example.lottery_system.dao.dataobject.ActivityUserDO;
import com.example.lottery_system.dao.mapper.ActivityUserMapper;
import com.example.lottery_system.service.dto.ConvertActivityStatusDTO;
import com.example.lottery_system.service.enums.ActivityUserStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatus targetStatus = convertActivityStatusDTO.getTargetActivityUserStatus();

        if(null == activityId
                || CollectionUtil.isEmpty(userIds)
                || null == targetStatus) {
            return false;
        }

        List<ActivityUserDO> activityUserDOList = activityUserMapper.batchSelectByActivityIdAndUserIds(activityId, userIds);
        if(CollectionUtil.isEmpty(activityUserDOList)) {
            return false;
        }
        for(ActivityUserDO activityUserDO : activityUserDOList) {
            if(activityUserDO.getStatus()
                    .equalsIgnoreCase(targetStatus.name())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatus targetStatus = convertActivityStatusDTO.getTargetActivityUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId, userIds, targetStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
